﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
using System.Xml;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting;
using Prism.Commands;
using YJTTool.Common.Helper;
using YJTTool.Core;

namespace YJTTool.Configure.ViewModels
{
    public class JsonConversionViewModel : InheritBase
    {
        #region 属性
        private string _inputText;
        private string _outputText;
        private string _statusMessage;
        private int _editorLineCount;
        private string _syntaxHighlightingName = "JSON";
        private DispatcherTimer _lineCountTimer;
        private Encoding _editorEncoding = Encoding.UTF8;
        public int EditorLineCount
        {
            get => _editorLineCount;
            set
            {
                _editorLineCount = value;
                RaisePropertyChanged();
            }
        }
        public Encoding EditorEncoding
        {
            get => _editorEncoding;
            set
            {
                _editorEncoding = value;
                RaisePropertyChanged();
            }
        }
        public string StatusMessage
        {
            get => _statusMessage;
            set
            {
                _statusMessage = value;
                RaisePropertyChanged();
            }
        }

        public string SyntaxHighlightingName
        {
            get => _syntaxHighlightingName;
            set
            {
                _syntaxHighlightingName = value;
                RaisePropertyChanged();
            }
        }
        public string InputText
        {
            get => _inputText;
            set
            {
                _inputText = value;
                RaisePropertyChanged();
                ValidateInput();
            }
        }

        public string OutputText
        {
            get => _outputText;
            set
            {
                _outputText = value;
                RaisePropertyChanged();
            }
        }
        #endregion
        #region 命令
        public DelegateCommand FormatCommand { get; }
        public DelegateCommand MinifyCommand { get; }
        public DelegateCommand ToXmlCommand { get; }
        public DelegateCommand ToYamlCommand { get; }
        #endregion
        #region ctor
        public JsonConversionViewModel()
        {
            _lineCountTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(500),
                IsEnabled = false
            };
            _lineCountTimer.Tick += (s, e) =>
            {
                _lineCountTimer.Stop();
            };
            FormatCommand = new DelegateCommand(FormatJson);
            MinifyCommand = new DelegateCommand(MinifyJson);
            ToXmlCommand = new DelegateCommand(ConvertToXml);
            ToYamlCommand = new DelegateCommand(ConvertToYaml);
        }
        #endregion

        #region 方法
        private void ValidateInput()
        {
            StatusMessage = JsonConverterHelper.ValidateJson(InputText)
                ? "JSON有效"
                : "JSON格式错误";
        }
        private void FormatJson()
        {
            try
            {
                SyntaxHighlightingName = "JSON";
                OutputText = JsonConverterHelper.FormatJson(InputText);
                StatusMessage = "格式化成功";
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        private void MinifyJson()
        {
            try
            {
                SyntaxHighlightingName = "JSON";
                OutputText = JsonConverterHelper.MinifyJson(InputText);
                StatusMessage = "压缩成功";
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        private void ConvertToXml()
        {
            try
            {
                SyntaxHighlightingName = "XML";
                OutputText = JsonConverterHelper.JsonToXml(InputText);
                StatusMessage = "XML转换成功";
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        private void ConvertToYaml()
        {
            try
            {
                SyntaxHighlightingName = "YAML";
                OutputText = JsonConverterHelper.JsonToYaml(InputText);
                StatusMessage = "YAML转换成功";
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        private void HandleError(Exception ex)
        {
            OutputText = $"错误：{ex.Message}";
            StatusMessage = "转换失败";
        }
        #endregion
    }
}
